package com.bq.gametrade.adapter;

import android.content.Context;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.AddressBookDetails;
import com.bq.gametrade.bean.FriendsListDetails;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.SharedPreferencesUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by Feel on 2016/12/27.
 */

public class FriendListAdapter extends BaseAdapter {

    private ArrayList<FriendsListDetails> mDatas;
    private Context context;

    public FriendListAdapter(Context context, ArrayList<FriendsListDetails> list) {
        this.mDatas = list;
        this.context = context;

    }

    public void setData(ArrayList<FriendsListDetails> list) {
        this.mDatas = list;

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.listview_friendlist_item, null);
            viewHolder.tvFriendNickName = (TextView) view.findViewById(R.id.tv_friendNickName);
            viewHolder.tvFriendPhoneNumber = (TextView) view.findViewById(R.id.tv_friendPhoneNumber);
            viewHolder.ivHeadImage = (ImageView) view.findViewById(R.id.civ_friendHeadImage);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        FriendsListDetails details = mDatas.get(position);
        if (!details.userName.equals("")) {
            if (!details.remark.equals("")) {
                viewHolder.tvFriendNickName.setText(Base64Util.decodeStr(details.remark)+"("+Base64Util.decodeStr(details.userName)+")");
            } else {
                viewHolder.tvFriendNickName.setText(Base64Util.decodeStr(details.userName));
            }
        } else {
            viewHolder.tvFriendNickName.setText(details.bqAccount);
            viewHolder.tvFriendNickName.setTextColor(context.getResources().getColor(R.color.color666666));
        }
        viewHolder.tvFriendPhoneNumber.setText(details.bqAccount);


        Glide.with(context).load(details.headimg).error(R.mipmap.user_headimg_gray).into(viewHolder.ivHeadImage);
        return view;
    }

    class ViewHolder {
        TextView tvFriendNickName;
        TextView tvFriendPhoneNumber;
        ImageView ivHeadImage;

    }
}
